package com.zhhs.galaxyott.activity;

import java.util.ArrayList;
import java.util.List;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.drawable.AnimationDrawable;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.zhhs.galaxyott.R;
import com.zhhs.galaxyott.adapters.HomeMovieListAdapter;
import com.zhhs.galaxyott.beans.HomeMovieBean;
import com.zhhs.galaxyott.customview.VideoPlayView;
import com.zhhs.galaxyott.customview.VideoPlayView.PlayerCallback;
import com.zhhs.galaxyott.customview.myListView;
import com.zhhs.galaxyott.utils.Logs;

/**
 * 
 * @author lc
 * @created 2016年6月29日 注：此Activity已被替代
 */
public class MainActivity extends BaseActivity implements OnClickListener {

    private static final String TAG = MainActivity.class.getSimpleName();

    public static int CurrentListPostion = 0;

    View containerView;
    TextView push_txt;
    View home_movie;
    VideoPlayView player;
    ImageView loadImg;
    ImageView vip_sort, hotmovie_sort, moremovie_sort, home_movieImg;
    ImageView history, personal, topic_sort, activity_sort, preview_sort;

    // 此版本没有用Listview来设置电影列表，如果要实现效果图中的效果则会出现反应迟钝的现象
    myListView mListView;
    HomeMovieListAdapter mAdapter;

    LinearLayout moviesLayout;
    TextView moviename_1, moviename_2, moviename_3, moviename_4, moviename_5, moviename_6, moviename_7;
    List<String> listData;
    List<HomeMovieBean> listDatas;

    private String urlString;

    @Override
    protected void dealLogicBeforeInitView() {
        super.dealLogicBeforeInitView();
        listData = new ArrayList<String>();
        listData.add("藏龙卧虎1");
        listData.add("藏龙卧虎2");
        listData.add("藏龙卧虎3");
        listData.add("藏龙卧虎4");
        listData.add("藏龙卧虎5");
        listData.add("藏龙卧虎6");
    }

    @Override
    protected void onCreateView() {
        setContentView(R.layout.activity_main);

        listDatas = new ArrayList<HomeMovieBean>();
        initView();
        setNextFocus(moviename_1);

        moviename_1.setTextColor(getResources().getColor(R.color.text_white));
        Logs.logD("DEBUG_LC", "THIS IS -->onCreateView()");
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        Logs.logD("DEBUG_LC", "THIS IS -->onRestart()");
        initPreview(urlString, "1");
    }

    @Override
    protected void onResume() {
        super.onResume();

        // initPreview(urlString);
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    protected void onStop() {
        loadImg.setVisibility(View.GONE);
        player.stopPlayer();
        Logs.logD("DEBUG_LC", "THIS IS -->onStop()");
        super.onStop();
    }

    private void initView() {

        containerView = findViewById(R.id.container);
        push_txt = (TextView) findViewById(R.id.push_txt);
        push_txt.setSelected(true);

        home_movie = findViewById(R.id.homepage_movie);
        loadImg = (ImageView) findViewById(R.id.load_bar);

        home_movieImg = (ImageView) findViewById(R.id.homepage_movieImg);
        player = (VideoPlayView) findViewById(R.id.homepage_movieVideo);
        urlString = "http://222.171.246.247:28080/ott/fileUpload/smiletv/zp/zkfm.mp4";
        initPreview(urlString, "1");

        vip_sort = (ImageView) findViewById(R.id.homepage_vip);
        hotmovie_sort = (ImageView) findViewById(R.id.homepage_hotmovie);
        moremovie_sort = (ImageView) findViewById(R.id.homepage_moremovie);
        history = (ImageView) findViewById(R.id.homepage_history);
        personal = (ImageView) findViewById(R.id.homepage_personal);
        topic_sort = (ImageView) findViewById(R.id.homepage_topic);
        activity_sort = (ImageView) findViewById(R.id.homepage_activity);
        preview_sort = (ImageView) findViewById(R.id.homepage_preview);

        initMovieLayout();

        vip_sort.requestFocus();

        home_movie.setOnClickListener(this);

        vip_sort.setOnClickListener(this);
        hotmovie_sort.setOnClickListener(this);
        moremovie_sort.setOnClickListener(this);
        history.setOnClickListener(this);
        personal.setOnClickListener(this);
        topic_sort.setOnClickListener(this);
        activity_sort.setOnClickListener(this);
        preview_sort.setOnClickListener(this);

    }

    private void initMovieLayout() {

        moviesLayout = (LinearLayout) findViewById(R.id.homepage_movieList);
        moviename_1 = (TextView) findViewById(R.id.moviename_1);
        moviename_2 = (TextView) findViewById(R.id.moviename_2);
        moviename_3 = (TextView) findViewById(R.id.moviename_3);
        moviename_4 = (TextView) findViewById(R.id.moviename_4);
        moviename_5 = (TextView) findViewById(R.id.moviename_5);

        moviename_1.setText(listData.get(0));
        moviename_2.setText(listData.get(1));
        moviename_3.setText(listData.get(2));
        moviename_4.setText(listData.get(3));
        moviename_5.setText(listData.get(4));

        int count = moviesLayout.getChildCount();
        Logs.logD("DEBUG_LL", "THIS IS initMovieLayout-->" + count);

    }

    private void initPreview(String url, String type) {
        if (url != null && type.equals("0")) {
            home_movieImg.setVisibility(View.GONE);
            loadImg.setVisibility(View.VISIBLE);
            loadImg.setImageResource(R.drawable.load_animation);
            ((AnimationDrawable) loadImg.getDrawable()).start();

            player.setVisibility(View.VISIBLE);
            player.setPlayerCallbackListener(mCallback);
            player.setVideoPath(url);
        } else {
            Toast.makeText(getApplicationContext(), type, Toast.LENGTH_SHORT).show();
            if (player.isShown()) {
                player.setVisibility(View.GONE);
                loadImg.setVisibility(View.GONE);
                player.stopPlayback();
                home_movieImg.setVisibility(View.VISIBLE);
            }

        }

    }

    OnFocusChangeListener mOnFocusChangeListener = new OnFocusChangeListener() {

        @SuppressLint("NewApi")
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if (hasFocus) {
                Logs.logD("DEBUG_L", "THIS IS onFocusChange--->1");
                v.setBackgroundResource(R.drawable.border_select);

            } else {
                v.setBackgroundResource(R.drawable.border_normal);
            }
        }
    };

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        switch (keyCode) {
        case KeyEvent.KEYCODE_DPAD_RIGHT:
            if (moviesLayout.findFocus() != null) {
                setNextFocus(moviesLayout.findFocus());

            }

            break;
        case KeyEvent.KEYCODE_DPAD_LEFT:
            if (moviesLayout.findFocus() != null) {
                setNextFocus(moviesLayout.findFocus());

            }

            break;
        case KeyEvent.KEYCODE_DPAD_DOWN:
            if (moviesLayout.findFocus() != null) {
                setMovieSelect("1");
                if (moviesLayout.findFocus() == moviename_5) {
                    setNextFocus(moviename_5);

                }
            }

            break;

        case KeyEvent.KEYCODE_DPAD_UP:
            if (moviesLayout.findFocus() != null) {
                setMovieSelect("-1");
            }

            break;

        default:
            break;
        }

        return super.onKeyDown(keyCode, event);
    }

    private void setMovieSelect(String string) {
        if (string.equals("1")) {
            if (moviesLayout.findFocus() == moviename_1) {
                initPreview(urlString, listData.get(1));
                moviename_1.setTextColor(getResources().getColor(R.color.homepage_list_line));
                moviename_2.setTextColor(getResources().getColor(R.color.text_white));
            } else if (moviesLayout.findFocus() == moviename_2) {
                initPreview(urlString, listData.get(2));
                moviename_2.setTextColor(getResources().getColor(R.color.homepage_list_line));
                moviename_3.setTextColor(getResources().getColor(R.color.text_white));
            } else if (moviesLayout.findFocus() == moviename_3) {
                initPreview(urlString, listData.get(3));
                moviename_3.setTextColor(getResources().getColor(R.color.homepage_list_line));
                moviename_4.setTextColor(getResources().getColor(R.color.text_white));
            } else if (moviesLayout.findFocus() == moviename_4) {
                initPreview(urlString, listData.get(4));
                moviename_4.setTextColor(getResources().getColor(R.color.homepage_list_line));
                moviename_5.setTextColor(getResources().getColor(R.color.text_white));
            }
        } else {
            if (moviesLayout.findFocus() == moviename_2) {
                initPreview(urlString, listData.get(0));
                moviename_2.setTextColor(getResources().getColor(R.color.homepage_list_line));
                moviename_1.setTextColor(getResources().getColor(R.color.text_white));
            } else if (moviesLayout.findFocus() == moviename_3) {
                initPreview(urlString, listData.get(1));
                moviename_3.setTextColor(getResources().getColor(R.color.homepage_list_line));
                moviename_2.setTextColor(getResources().getColor(R.color.text_white));
            } else if (moviesLayout.findFocus() == moviename_4) {
                initPreview(urlString, listData.get(2));
                moviename_4.setTextColor(getResources().getColor(R.color.homepage_list_line));
                moviename_3.setTextColor(getResources().getColor(R.color.text_white));

            } else if (moviesLayout.findFocus() == moviename_5) {
                initPreview(urlString, listData.get(3));
                moviename_5.setTextColor(getResources().getColor(R.color.homepage_list_line));
                moviename_4.setTextColor(getResources().getColor(R.color.text_white));

            }
        }

    }

    private void setNextFocus(View view) {
        home_movie.setNextFocusRightId(view.getId());
        history.setNextFocusLeftId(view.getId());
        topic_sort.setNextFocusLeftId(view.getId());
        activity_sort.setNextFocusLeftId(view.getId());
        moremovie_sort.setNextFocusUpId(view.getId());
    }

    @Override
    public void onClick(View v) {
        Intent intent = new Intent();

        switch (v.getId()) {
        case R.id.homepage_movie:
            intent.setClass(this, MovieDetailActivity.class);
            startActivity(intent);

            break;
        case R.id.homepage_vip:

            break;
        case R.id.homepage_hotmovie:
            intent.setClass(this, HotMovieActivity.class);
            startActivity(intent);
            break;
        case R.id.homepage_moremovie:
            intent.setClass(this, MoreMoviesActivity.class);
            startActivity(intent);
            break;
        case R.id.homepage_history:
            intent.setClass(this, DeviceCenterActivity.class);
            intent.putExtra("number", 1);
            startActivity(intent);
            break;
        case R.id.homepage_personal:
            intent.setClass(this, DeviceCenterActivity.class);
            intent.setClass(this, DeviceCenterActivity.class);
            intent.putExtra("number", 0);
            startActivity(intent);

            break;
        case R.id.homepage_topic:
            intent.setClass(this, MoreSpecialActivity.class);
            startActivity(intent);

            break;
        case R.id.homepage_activity:
            intent.setClass(this, SingleSpecialActivity.class);
            startActivity(intent);
            break;
        case R.id.homepage_preview:

            break;

        default:
            break;
        }
    }

    PlayerCallback mCallback = new PlayerCallback() {

        @Override
        public void onPrepared(int width, int height) {
            Logs.logD("DEBUG_LC", "THIS IS onPrepared-->");
            player.start();
            loadImg.setVisibility(View.GONE);
        }

        @Override
        public boolean onInfo(int what, int extra) {
            Logs.logD("DEBUG_LC", "THIS IS onInfo-->");
            return false;
        }

        @Override
        public void onCompletion() {
            // TODO Auto-generated method stub

        }

        @Override
        public boolean onError(int framework_err, int impl_err) {
            // TODO Auto-generated method stub
            return false;
        }

        @Override
        public void onCloseMusic() {
            // TODO Auto-generated method stub

        }

        @Override
        public void onBufferingUpdate(int percent) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onStopPlayer() {
            // TODO Auto-generated method stub

        }

        @Override
        public void onSeekComplete() {
            // TODO Auto-generated method stub

        }

        @Override
        public void onPlayEnd() {
            // TODO Auto-generated method stub

        }

        @Override
        public void onPauseComplete() {
            // TODO Auto-generated method stub

        }

        @Override
        public void onFinish() {
            // TODO Auto-generated method stub

        }

    };

}
